//
//  UIImage+Optimize.m
//  CheFu365
//
//  Created by safiri on 2017/10/28.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "UIImage+Optimize.h"

@implementation UIImage (Optimize)

- (UIImage *)zsScaleImageWithSize:(CGSize)size {
    
    if (CGSizeEqualToSize(size, self.size)) {
        return self;
    }
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
    
    //设置rect
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    //将图片绘制到图形上下文
    [self drawInRect:rect];
    
    //从图形上下文获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    //返回图片
    return image;
}

- (void)zsScaleImageWithSize:(CGSize)size completion:(void(^)(UIImage *img))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        UIImage *image = [self zsScaleImageWithSize:size];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(image);
            }
        });
    });
}

// 生成圆角图片(同步)
- (UIImage *)zsCircleImageWithSize:(CGSize)size fillClolor:(UIColor *)fillColor {
    
    // 获取开始时间
    //CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    // 开启图形上下文
    UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
    
    // 设置rect
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    // 设置填充色
    [fillColor set];
    UIRectFill(rect);
    
    // 计算半径
    CGFloat cornerRadius = MIN(size.width, size.height) * 0.5;
    
    // 设置圆形路径并切割
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius] addClip];
    
    // 将原始图片绘制到图形上下文中
    [self drawInRect:rect];
    
    // 从图形上下获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    // 获取结束时间
    //CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    
    //打印时间
    //NSLog(@"%f",end - start);
    
    // 返回圆形图片
    return image;
}
- (void)zsCornerImageWithSize:(CGSize)size fillClolor:(UIColor *)fillColor completion:(void(^)(UIImage *img))completion {
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        UIImage *image = [self zsCircleImageWithSize:size fillClolor:fillColor];
        
        // 返回圆形图片
        // return image;
        // 返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(image);
            }
        });
        
    });
    
}
- (UIImage *)zsCircleImageFillClolor:(UIColor *)fillColor {
    return [self zsCircleImageWithSize:self.size fillClolor:fillColor];
}
- (void)zsCircleImageFillClolor:(UIColor *)fillColor completion:(void(^)(UIImage *img))completion {
    [self zsCornerImageWithSize:self.size fillClolor:fillColor completion:completion];
}

- (UIImage *)zsCircleImageWithSize:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    
    // 获取上下文
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    
    // 设置圆形
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    
    CGContextAddEllipseInRect(ctr, rect);
    
    // 裁剪
    CGContextClip(ctr);
    
    // 将图片画上去
    [self drawInRect:rect];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
- (void)zsCircleImageWithSize:(CGSize)size completion:(void(^)(UIImage *img))completion {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        UIImage *image = [self zsCircleImageWithSize:size];
        
        // 返回圆形图片
        // return image;
        // 返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(image);
            }
        });
        
    });
}

- (UIImage *)zsCircleImage {
    return [self zsCircleImageWithSize:self.size];
}
- (void)zsCircleImageCompletion:(void(^)(UIImage *img))completion {
    [self zsCircleImageWithSize:self.size completion:completion];
}
@end
/*
 UIGraphicsBeginImageContext
 创建一个基于位图的上下文（context）,并将其设置为当前上下文(context)。方法声明如下：
 void UIGraphicsBeginImageContext(CGSize size);
 参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。
 该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同，相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。
 
 UIGraphicsBeginImageContextWithOptions
 函数原型为：
 void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
 size——同UIGraphicsBeginImageContext
 opaque—透明开关，如果图形完全不用透明，设置为YES以优化位图的存储。
 scale—–缩放因子 iPhone 4是2.0，其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取，但实际上设为0后，系统就会自动设置正确的比例了。
 */
